/**
 * @date 10:19:43 AM
 * GroupController.java
 * Administrator
 * TODO
 */
package com.qs.education.controllers;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationGroupResponse;
import com.qs.education.bean.entity.EducationGroup;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.utils.JsonUtil;
import com.qs.education.service.IGroupAPIService;

/**
 * @class GroupController
 * @author Warren
 * @description
 * @date Nov 8, 2018
 */
@RestController
public class GroupController {

	@Autowired
	private IGroupAPIService iGroupAPIService;
	private final Logger log = Logger.getLogger(this.getClass());

	/**
	 * @author Warren
	 * @param id
	 * @return EducationGroupResponse
	 * @description id为群号(1000001开始增长)，根据群号搜索班群
	 * @date Nov 13, 2018
	 */

	@RequestMapping("/group/searchByIdAPI")
	public EducationGroupResponse getList(Long id) {
		log.info("api");
		EducationGroupResponse educationGroupResponse = iGroupAPIService.searchById(id);
		return educationGroupResponse;
	}

	/**
	 * @author Warren
	 * @param userId 申请的用户id
	 * @param groupId 申请的班群号
	 * @param sendType 申请类型,教师或学生
	 * @return ResponseBean
	 * @description 申请加入班群
	 * @date Nov 19, 2018
	 */
	@RequestMapping("/group/applyAPI")
	public ResponseBean apply(Long userId, Long groupId, int sendType) {
		log.info("api");
		ResponseBean responseBean = null;
		try {
			responseBean = iGroupAPIService.apply(userId, groupId, sendType);
		}catch(Exception e) {
			responseBean = new ResponseBean();
			responseBean.setStatus(ResponseStatus.FAILED.toString());
		}
		return responseBean;
	}
	
	/**
	 * @author Warren
	 * @param id 申请班群消息id
	 * @param messageStatus 教师处理结果
	 * @return ResponseBean
	 * @description 教师处理班群申请
	 * @date Nov 13, 2018
	 */
	@RequestMapping("/group/applyHandleAPI")
	public ResponseBean applyHandle(Long id,int messageStatus) {
		log.info("api");
		ResponseBean responseBean = null;
		try {
			responseBean = iGroupAPIService.applyHandle(id, messageStatus);
			responseBean.setStatus(ResponseStatus.SUCCESS.toString());
		}catch(Exception e) {
			responseBean = new ResponseBean();
			responseBean.setStatus(ResponseStatus.FAILED.toString());
		}
		return responseBean;
	}
	
	/**
	 * @author Warren
	 * @param studentId 学生id
	 * @return ResponseListBean<EducationGroup>
	 * @description 根据学生id获取学生的班群列表
	 * @date Nov 13, 2018
	 */
	@RequestMapping("/group/findGroupByStudentIdAPI")
	public ResponseListBean<EducationGroup> findGroupByStudentId(Long studentId) {
		ResponseListBean<EducationGroup> responseListBean = iGroupAPIService.findGroupByStudentId(studentId);
		return responseListBean;
	}
	
	/**
	 * @author Warren
	 * @param ids 移除学生的id集合 
	 * @return ResponseBean
	 * @description 批量移除学生
	 * @date Nov 13, 2018
	 */
	@RequestMapping("/group/removeStudentsAPI")
	public ResponseBean removeStudents(String ids,String groupId){
		ResponseBean responseBean = iGroupAPIService.removeStudents(ids,groupId);
		return responseBean;
	}
	
	/**
	 * @Description 根据教师ID 查询所有的群组
	 * @author Leon
	 * @date Nov 9, 2018
	 * @param
	 * @return ResponseListBean<EducationGroupResponse>
	 */
	@RequestMapping("/group/findGroupByTeacherId")
	public ResponseListBean<EducationGroupResponse> findGroupByTeacherId(HttpServletRequest request,int teacherId) {
		log.info("api controller findGroupByTeacherId");
		System.out.println("api controller findGroupByTeacherId " + teacherId);
		ResponseListBean<EducationGroupResponse> response = iGroupAPIService.findGroupByTeacherId(teacherId);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 教师创建 班群
	 * @author Leon
	 * @date Nov 12, 2018
	 * @param
	 * @return EducationGroupResponse
	 */
	@RequestMapping("/group/createGroups")
	public EducationGroupResponse createGroups(HttpServletRequest request, String params) {
		log.info("api controller createGroups ");
		EducationGroupResponse response = new EducationGroupResponse();
		response = iGroupAPIService.createGroups(params);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 移除教师
	 * @author Leon
	 * @date   Nov 13, 2018
	 * @param  
	 * @return EducationGroupResponse
	 */
	@RequestMapping("/group/removeTeacher")
	public EducationGroupResponse removeTeacher(HttpServletRequest request,String teacherId,String groupId) {
		log.info("api controller removeTeacher");
		EducationGroupResponse response = new EducationGroupResponse();
		response = iGroupAPIService.removeTeacher(teacherId,groupId);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 转让班群
	 * @author Leon
	 * @date   Nov 13, 2018
	 * @param  
	 * @return EducationGroupResponse
	 */
	@RequestMapping("/group/transferGroup")
	public EducationGroupResponse transferGroup(HttpServletRequest request,String teacherId,String groupId) {
		log.info("api controller transferGroup");
		EducationGroupResponse response = new EducationGroupResponse();
		response = iGroupAPIService.transferGroup(teacherId, groupId);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 修改群组
	 * @author Leon
	 * @date   Nov 14, 2018
	 * @param  
	 * @return EducationGroupResponse
	 */
	@RequestMapping("/group/modifyGroup")
	public EducationGroupResponse modifyGroup(HttpServletRequest request,String param) {
		log.info("api controller modifyGroup");
		EducationGroupResponse response = new EducationGroupResponse();
		EducationGroup group = (EducationGroup) JsonUtil.jsonToBean(param, EducationGroup.class);
		System.out.println("=========="+group.toString());
		response = iGroupAPIService.modifyGroup(group);
		response.setToken(response.getToken(request));
		return response;
	}
	
	/**
	 * @Description 删除群组
	 * @author Leon
	 * @date   Nov 14, 2018
	 * @param  
	 * @return EducationGroupResponse
	 */
	@RequestMapping("/group/deleteGroupById")
	public EducationGroupResponse deleteGroupById(HttpServletRequest request,String groupId) {
		log.info("api controlle deleteGroupById");
		System.out.println("groupId :" + groupId);
		EducationGroupResponse response = new EducationGroupResponse();
		response = iGroupAPIService.deleteGroupById(groupId);
		response.setToken(response.getToken(request));
		return response;
	}

	/**
	 * @Description 查询所有符合条件的群组
	 * @author Jason
	 * @date Nov 20, 2018
	 * @param request
	 * @param schoolName
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/group/getGroupAPI")
	public PageBean getGroupList(HttpServletRequest request,
			String schoolName,Integer currentPage,Integer pageSize){
		
		log.info("api Group 端 Controller  getGroupList");
		PageBean response = new PageBean();
		response.setToken(response.getToken(request));
		response = iGroupAPIService.getGroupList(schoolName,currentPage,pageSize);
		response.setStatus(ResponseStatus.SUCCESS.toString());
		
		return response;
	}
}